package com.luo90.nodecfg.controller.node;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.ComboTree;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.easyui.ComboTreeModel;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSFunction;
import org.jeecgframework.web.system.pojo.base.TSRole;
import org.jeecgframework.web.system.pojo.base.TSRoleFunction;
import org.jeecgframework.web.system.pojo.base.TSRoleUser;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.luo90.nodecfg.entity.node.NodeAttrEntity;
import com.luo90.nodecfg.entity.node.NodeEntity;
import com.luo90.nodecfg.service.node.NodeServiceI;

/**   
 * @Title: Controller
 * @Description: 活动节点管理
 * @author zhangdaihao
 * @date 2016-01-25 18:14:28
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/nodeController")
public class NodeController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(NodeController.class);

	@Autowired
	private NodeServiceI nodeService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 活动节点管理列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "node")
	public ModelAndView node(HttpServletRequest request) {
		return new ModelAndView("com/luo90/nodecfg/node/nodeList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "select")
	public List<ComboTree> select(NodeEntity node,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(NodeEntity.class, dataGrid);
		cq.eq("aviFlag", "1");
		List<NodeEntity> nodeList = systemService.getListByCriteriaQuery(
				cq, false);
		List<ComboTree> comboTrees = new ArrayList<ComboTree>();
		ComboTreeModel comboTreeModel = new ComboTreeModel("id",
				"name", "");
		comboTrees = systemService.ComboTree(nodeList, comboTreeModel,
				null, false);
		MutiLangUtil.setMutiTree(comboTrees);
		return comboTrees;
		
	}
	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(NodeEntity node,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(NodeEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, node, request.getParameterMap());
		this.nodeService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**

	 * 角色所有用户信息列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "userList")
	public ModelAndView userList(HttpServletRequest request) {
		request.setAttribute("roleId", request.getParameter("roleId"));
		return new ModelAndView("system/role/roleUserList");
	}
	
	
	
	/**
	 * 删除活动节点管理
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(NodeEntity node, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		node = systemService.getEntity(NodeEntity.class, node.getId());
		delNodeAttr(node);
		message = "活动节点管理删除成功";
		nodeService.delete(node);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}

	/**
	 * 删除节点属性
	 * 
	 * @param role
	 */
	protected void delNodeAttr(NodeEntity node) {
		List<NodeAttrEntity> nodeAttrs = systemService.findByProperty(
				NodeAttrEntity.class, "NodeAttrEntity.nodeId", node.getId());
		if (nodeAttrs.size() > 0) {
			for (NodeAttrEntity nodeAttr : nodeAttrs) {
				systemService.delete(nodeAttr);
			}
		}
	}
	/**
	 * 添加活动节点管理
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(NodeEntity node, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(node.getId())) {
			message = "活动节点管理更新成功";
			NodeEntity t = nodeService.get(NodeEntity.class, node.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(node, t);
				nodeService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "活动节点管理更新失败";
			}
		} else {
			message = "活动节点管理添加成功";
			nodeService.save(node);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 活动节点管理列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(NodeEntity node, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(node.getId())) {
			node = nodeService.getEntity(NodeEntity.class, node.getId());
			req.setAttribute("nodePage", node);
		}
		return new ModelAndView("com/luo90/nodecfg/node/node");
	}
}
